﻿using System;
using System.Collections.Generic;
using System.Windows;
using System.Windows.Controls;
using Resn.WhuPub.BIL;
using Resn.WhuPub.BLL;

namespace Resn.WhuPub.WPFClientUI
{
        /// <summary>
        /// CreateSubjectWindow.xaml 的交互逻辑
        /// </summary>
        public partial class CreateSubjectPage : Page
        {
                private readonly GlobalManager gm = GlobalManager.Instance;

                public CreateSubjectPage()
                {
                        InitializeComponent();
                }

                private void Page_Loaded(object sender, RoutedEventArgs e)
                {
                        LayoutRoot.DataContext = gm;
                }

                private void AcceptButton_Click(object sender, RoutedEventArgs e)
                {
                        if (String.IsNullOrEmpty(gm.SubjectName))
                        {
                                MessageBox.Show("科目名称不能为空");
                                return;
                        }

                        if (String.IsNullOrEmpty(gm.SubjectCode))
                        {
                                MessageBox.Show("科目代码不能为空");
                                return;
                        }

                        try
                        {
                                ISubject subject =
                                        gm.Category.CreateSubject(gm.SubjectName,gm.SubjectCode);
                                subject.Insert();
                        }
                        catch (Exception ex)
                        {
                                MessageBox.Show(ex.Message);
                                return;
                        }

                        finally
                        {
                        }

                        gm.Subjects = gm.Category.Subjects;
                        NavigationService.Navigate(new Uri("ManageSubjectsPage.xaml", UriKind.Relative));
                }

                
        }
}